package com.yun.algorithmproblem.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Leetcode47 {

    public static void main(String[] args) {
        Leetcode47 obj=new Leetcode47();
        int[] nums=new int[]{1,1,2};
        List<List<Integer>> lists = obj.permuteUnique(nums);
        System.out.println(lists);
    }


    public List<List<Integer>> permuteUnique(int[] nums) {
        int[] bucket = new int[21];
        for (int num : nums) {
            bucket[num + 10]++;
        }
        List<List<Integer>> result = new ArrayList<>();
        dfs(bucket, nums.length, new LinkedList<>(), result);
        return result;
    }

    private void dfs(int[] bucket, int length,
                     LinkedList<Integer> stack,
                     List<List<Integer>> result) {
        if (stack.size() == length) {
            result.add(new ArrayList<>(stack));
            return;
        }
        for (int i = 0; i <= 20; i++) {
            if(bucket[i]>0){
                stack.push(i - 10);
                bucket[i]--;
                dfs(bucket, length, stack, result);
                bucket[i]++;
                stack.pop();
            }
        }
    }
}
